<?php

class Model_User_Api extends Core_Model_Abstract
{
    public function getUidByMac($mac)
    {
        return Dao('User_Index')->getUidByMac($mac);
    }

    public function register($mac)
    {
        $uid = Dao('User_Index')->generateUid();

        // demo
        $userName = Helper_Array::rand(array(
            '阿诺·施瓦辛格',
            '史蒂夫·弗朗西斯',
            '妮可·罗宾',
            '丹尼斯·罗德曼',
            '迈克·杰克逊',
            '索隆·罗密欧',
            '康桑·思密达'
        ));

        // demo
        $countryId = rand(1, 4);

        // 用户索引表
        $setArr = array(
            'uid' => $uid,
            'mac' => $mac,
            'user_name' => $userName,
            'db_suffix' => $this->_getDbSuffixForNewUser($uid),
            'level_id' => 1,
            'position_id' => 1,
            'country_id' => $countryId,
        );
        Dao('User_Index')->insert($setArr);

        // 用户基本信息
        $setArr = array(
            'uid' => $uid,
            'mac' => $mac,
            'user_name' => $userName,
            'create_time' => $GLOBALS['_TIME'],
            'silver' => 10000,
            'gold' => 600,
            'move' => 200,
            'move_max' => 200,
            'hp' => 100,
            'hp_max' => 100,
            'energy' => 3,
            'energy_max' => 3,
            'exp' => 0,
            'level_id' => 1,
            'position_id' => 0,
            'country_id' => $countryId,
            'avatar_id' => 1,
            'sea_area_id' => 1,
            'status' => 0,
            'port_from' => 1, // 默认港口
        );
        Dao('User_Info')->loadDs($uid)->insert($setArr);

        return $uid;
    }

    private function _getDbSuffixForNewUser($uid)
    {
        $suffixes = explode(',', DB_SUFFIX_NEW_USER);
        return Helper_Array::rand($suffixes, 1);
    }
}